package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.entity.Post;
import com.leoman.service.PostImageService;
import com.leoman.service.PostService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 帖子管理
 * Created by 涂奕恒 on 2016/10/28.
 */
@Controller
@RequestMapping(value = "backend/post")
public class PostController extends CommonController {

    @Autowired
    private PostService postService;

    @Autowired
    private PostImageService postImageService;

    @RequestMapping("index")
    public String index() {
        return "backend/帖子列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String content,
                     String nickName,
                     Integer checkStatus,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Post> page = postService.pageByParams(content, nickName, checkStatus, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("show")
    public String show(ModelMap model, Integer postId) {
        if (null != postId) {
            Post post = postService.getById(postId);
            model.addAttribute("post", post);
            model.addAttribute("createTime", DateUtils.longToStringWithFormat(post.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));

            // 查询帖子图片列表
            model.addAttribute("postImageList", postImageService.findListByPostId(postId));
        }

        return "backend/帖子详情";
    }

    /**
     * 审核帖子
     *
     * @param postId 帖子ID
     * @param status 状态
     * @return
     */
    @RequestMapping("/changeCheckStatus")
    @ResponseBody
    public Integer changeCheckStatus(Integer postId, Integer status) {
        try {
            Integer[] postIds = new Integer[1];
            postIds[0] = postId;
            return postService.changePostCheckStatus(postIds, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除帖子
     *
     * @param ids 帖子ID
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Integer batchDelete(String ids) {
        try {
            Integer[] postIds = JsonUtil.json2Obj(ids, Integer[].class);
            return postService.batchDelete(postIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除帖子
     *
     * @param postId 帖子ID
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer postId) {
        try {
            return postService.deleteByPostId(postId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
